package com.zyd.shiro.controller.admin;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.persistence.beans.TrainMeeting;
import com.zyd.shiro.persistence.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.TrainUser;
import com.zyd.shiro.business.service.MallTrainUserService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
/**
 * 控制器层
 * @author
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/trainUser")
public class TrainUserController {

	@Autowired
	private MallTrainUserService trainUserService;
	
	/**
	 * 查询全部数据，已测试
	 * @return
	 */
	@RequestMapping(value="/findAll/",method=RequestMethod.GET)
	public ResponseVO findAll(){
		 return ResultUtil.success("查询成功",trainUserService.findAll());
		
	}
	
	/**
	 * 根据ID查询，已测试
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/findById/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", trainUserService.findOne(id));

	}
	/**
	 * 按照培训的ID 查看报名用户信息, 已测试
	 *type 为查询用户的类型，0为查看报名但未交费用的用户，1为报名且缴费用户，2为全部用户
	 */
	@RequestMapping(value="/findAllByTrainId/{train_id}/{type}",method=RequestMethod.GET)
	public ResponseVO findAllByTrainId(@PathVariable Integer train_id,@PathVariable int type){
		return ResultUtil.success("查询成功",trainUserService.findAllByTrainId(train_id,type));
	}
	/**

	/**
	 * 按照用户信息获得所其所参加的所有培训, 已测试
	 */
	@RequestMapping(value="/findAllByTrainId/{user_id}",method=RequestMethod.GET)
	public ResponseVO findAllTByUserId(@PathVariable Integer user_id){
		return ResultUtil.success("查询成功",trainUserService.finAllByUserId(user_id));
	}
	/**
	 * 获得报名培训但未缴纳费用的人数, 已测试
	 */
	@RequestMapping(value="/findNotPayNum/{train_id}",method=RequestMethod.GET)
	public ResponseVO findNotPayNum(@PathVariable Integer train_id){

		return ResultUtil.success("报名但未缴费",trainUserService.findNotPayNum(train_id));
	}

	/**
	 * 获得报名培训且缴纳费用的人数, 已测试
	 * @param
	 * @return
	 */
	@RequestMapping(value="/findPayNum/{train_id}",method=RequestMethod.GET)
	public ResponseVO findPayNum(@PathVariable Integer train_id){

		return  ResultUtil.success("报名且缴费",trainUserService.countAllUserNumByTrainId(train_id));
	}


	/**
	 * 报名.已测试
	 */
	@RequestMapping(value="/Join/{train_id}/{user_id}",method=RequestMethod.POST)
	public ResponseVO Join(@PathVariable Integer train_id,@PathVariable Integer user_id){

		if(trainUserService.isJoinTrain(train_id , user_id))
		{
			return ResultUtil.error("报名失败，你已经报名了！");
		}
		else{
			if(trainUserService.countAllUserNumByTrainId(train_id)>=trainUserService.getTrainMaxNum(train_id))
				return ResultUtil.error("报名失败，参加人数已满！");
			else {
				TrainUser trainUser = new TrainUser();
				trainUser.setUser_id(user_id);
				trainUser.setTrain_id(train_id);
				trainUser.setCreate_time(new Date());
				trainUser.setIspay(0);
				trainUser.setTrain_title(trainUserService.getTrainName(train_id));
				trainUser.setUser_name(trainUserService.getUserName(user_id));
				trainUserService.add(trainUser);
				return ResultUtil.success("报名成功！");
			}
		}

	}


	/**
	 * 缴费
	 */

	/**
	 * 退款
	 */


	/**
	 * 按照会议id删除,已测试
	 */

	@RequestMapping(value="/DeleteByTrainId/{trainId}",method=RequestMethod.DELETE)
	public ResponseVO DeleteByTrainId(@PathVariable Integer trainId ){
		trainUserService.DeletByTrainId(trainId);
		return ResultUtil.success("删除成功");
	}

	/**
	 * 按照会议Id和用户id删除，已测试
	 */
	@RequestMapping(value="/DeleteByTrainId/{trainId}/{userId}",method=RequestMethod.DELETE)
	public ResponseVO DeleteByTrainIdAndUserId(@PathVariable Integer trainId,@PathVariable Integer userId ){
		trainUserService.DeleteByTrainIdAndUserId(trainId,userId);
		return ResultUtil.success("删除成功");
	}
	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size){		
		Page<TrainUser> pageList = trainUserService.findPage(page, size);		
		return ResultUtil.success("查询成功",new PageResult(pageList.getTotalElements(), pageList.getContent() ) );	
	}
	
	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
	public ResponseVO findSearch(@RequestBody Map searchMap ,@PathVariable int page,@PathVariable int size){		
		Page<TrainUser> pageList = trainUserService.findSearch(searchMap, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );		
	}
	
	/**
	 * 增加，已测试
	 * @param trainUser
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ResponseVO add(@RequestBody TrainUser trainUser  ) {
		trainUserService.add(trainUser);
		return ResultUtil.success("增加成功");
	}
	/**
	 * 修改，已测试
	 * @param trainUser
	 */
	@RequestMapping(value="/update/{id}",method=RequestMethod.PUT)
	public ResponseVO update(@RequestBody TrainUser trainUser,@PathVariable Integer id ){
		trainUser.setId(id);
		trainUserService.update(trainUser);		
		return ResultUtil.success("修改成功");
	}
	
	/**
	 * 删除，已测试
	 * @param id
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
	public ResponseVO delete(@PathVariable Integer id ){
		trainUserService.delete(id);
		return ResultUtil.success("删除成功");
	}
	
}
